package ee.fj.util.assertion;

public class AssertionChain extends Assertion {
	public static enum ConditionType {
		and, or;
	};


	private final Assertion condition;
	private final Assertion nextCondition;
	private final ConditionType type;

	public AssertionChain(Assertion condition, Assertion nextCondition, ConditionType type, boolean reverse) {
		super(reverse);
		this.condition = condition;
		this.nextCondition = nextCondition;
		this.type = type;
	}

	public AssertionChain(Assertion condition, Assertion nextCondition, ConditionType type) {
		this(condition, nextCondition, type, DEFAULT_CONDITION);
	}

	public AssertionChain(Assertion condition, Assertion nextCondition) {
		this(condition, nextCondition, ConditionType.and);
	}
	public AssertionChain(Assertion condition, Assertion nextCondition, boolean reverse) {
		this(condition, nextCondition, ConditionType.and, reverse);
	}
	public AssertionChain(Assertion condition, Assertion nextCondition, String type) {
		this(condition, nextCondition, ConditionType.valueOf(type.toLowerCase()));
	}
	public AssertionChain(Assertion condition, Assertion nextCondition, String type, boolean reverse) {
		this(condition, nextCondition, ConditionType.valueOf(type.toLowerCase()), reverse);
	}

	@Override
	protected boolean satisfiesInner(Object value) throws AssertionException {
		if (type.equals(ConditionType.or)) {
			return condition.satisfies(value) || nextCondition.satisfies(value);
		} else {
			return condition.satisfies(value) && nextCondition.satisfies(value);
		}
	}
}
